<?php
/**
 * Created by PhpStorm.
 * User: banyunba
 * Date: 2018/5/15
 * Time: 下午12:21
 */

namespace app\common\controller;


use think\Controller;
use think\Db;
use think\facade\Cache;
use think\facade\Env;

class CommonBase extends Controller
{
    protected $siteConfig;

    protected function initialize()
    {
        parent::initialize();

        $this->siteConfig = $this->getSystem();
        $this->assign("sys_config",$this->siteConfig);

        $runtime_path = Env::get('runtime_path');
        if ( delete_dir_file(	$runtime_path . 'temp/') ) {
//            echo "del";
        }


    }

    /**
     * 获取站点信息
     * @return array|mixed|null|\PDOStatement|string|\think\Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    protected function getSystem()
    {
        if (Cache::has('site_config')) {
            $site_config = Cache::get('site_config');
        } else {
            $site_config = Db::name('system')->field('value')->where('name', 'site_config')->find();
            $site_config = json_decode($site_config['value'],true);
            Cache::set('site_config', $site_config,1);
        }
        return $site_config;
    }

    /**
     * 输出json 数据
     * @param array $data
     * @param int $code
     * @param string $msg
     */
    protected function echoJson($data = array(),$code = 200,$msg = 'ok')
    {
        $data = array(
            'code' => $code,
            'msg' => $msg,
            'data' => $data
        );
        header('Content-Type: application/json; charset=utf-8');
        exit(json_encode($data));
    }


}